package com.ktjiaoyu.ktmall.dao.impl;

import com.ktjiaoyu.ktmall.dao.BaseDao;
import com.ktjiaoyu.ktmall.entity.Category;
import com.ktjiaoyu.ktmall.dao.CategoryDao;
import com.ktjiaoyu.ktmall.entity.Category;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CategoryDaoImpl extends BaseDao implements CategoryDao {

    // 删除
    @Override
    public int delete(Long id) {
        String sql = "delete from kt_category where id=?";
        try {
            return executeUpdate(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        } finally {
        }
    }

    // 查询
    @Override
    public List<Category> findAll() {
        String sql = "select * from kt_category where is_deleted=0 order by parent_id, sort";
        try {
            return executeQuery(sql, resultSet -> {
                List<Category> list = new ArrayList<Category>();
                try {
                    while (resultSet.next()) {
//                        Category category = new Category(1111);
//                        category.setId(resultSet.getLong("id"));
//                        category.setName(resultSet.getString("name"));
//                        category.setParent_id(resultSet.getLong("parent_id"));
//                        category.setIcon(resultSet.getString("icon"));
//                        category.setSort(resultSet.getInt("sort"));
//                        category.setStatus(resultSet.getInt("status"));
//                        category.setIs_deleted(resultSet.getInt("is_deleted"));
//                        category.setUpdate_time(resultSet.getString("update_time"));
//                        list.add(category);
                        list.add(extracCategory(resultSet));
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                } finally {
                }
                return list;
            });
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        } finally {
        }
    }

    //新增
    @Override
    public int insert(Category category) {
        String sql = "INSERT INTO `ktdb`.`kt_category`(`name`, `parent_id`, `icon`, `sort`, `status`) " +
                "VALUES (?,?,?,?,?);\n";
        try {
            return executeUpdate(sql,category.getName(),category.getParent_id(),category.getIcon(),category.getSort(),
                    category.getStatus());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }
    }

    // 修改
    @Override
    public int update(Category category) {
        StringBuilder sql = new StringBuilder("update kt_category set  ");
        List<Object> params = new ArrayList<>();
        if (category.getName() != null) {
            sql.append("name = ?,");
            params.add(category.getName());
        }
        if (category.getParent_id() != null) {
            sql.append("parent_id = ?,");
            params.add(category.getParent_id());
        }
        if (category.getIcon() != null) {
            sql.append("icon = ?,");
            params.add(category.getIcon());
        }
        if (category.getSort() != null) {
            sql.append("sort = ?,");
            params.add(category.getSort());
        }
        if (category.getStatus() != null) {
            sql.append("status = ?,");
            params.add(category.getStatus());
        }
        sql.append("update_time=now() where id=?");
        params.add(category.getId());

        System.out.println(sql.toString());
        try {
            return executeUpdate(sql.toString(),params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        } finally {
        }
    }

    private Category extracCategory(ResultSet resultSet) throws SQLException {
//        List<Category> list = new ArrayList<>();
        Category category = new Category();
        category.setId(resultSet.getString("id"));
        category.setName(resultSet.getString("name"));
        category.setParent_id(resultSet.getString("parent_id"));
        category.setIcon(resultSet.getString("icon"));
        category.setSort(resultSet.getInt("sort"));
        category.setStatus(resultSet.getInt("status"));
        category.setIs_deleted(resultSet.getInt("is_deleted"));
        category.setUpdate_time(resultSet.getString("update_time"));
//        category.setChildren(list.add(category));
        return category;
    }



}
